package com.youdoneed.common.controller;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.common.model.FileInfo;
import com.youdoneed.common.model.VideoScreenShotImg;
import com.youdoneed.common.service.FileService;
import com.youdoneed.community.shortVideo.entity.Musiclibrary;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ewing
 * @since 2017-04-21
 **/
@RestController
@Api(value = "文件管理", description = "文件管理")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 根据ID获取文件信息。
     */
    @ApiOperation("根据ID获取文件信息")
    @RequestMapping(value = "/getFileInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<FileInfo> getFileInfo(String fileId) {
        return new ResultMessage<>(fileService.getFileInfo(fileId));
    }

    /**
     * 根据ID批量获取文件信息。
     */
    @ApiOperation("根据ID批量获取文件信息")
    @RequestMapping(value = "/getFileInfos", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<FileInfo>> getFileInfos(String[] fileIds) {
        return new ResultMessage<>(fileService.getFileInfos(fileIds));
    }

    /**
     * 上传单个文件。
     */
    @ApiOperation("上传单个文件")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResultMessage<FileInfo> uploadFile(@RequestPart("file") MultipartFile file) {
        return new ResultMessage<>(fileService.saveFile(file));
    }

//    /**
//     * 上传原创音乐文件。(仅给IOS端提供m4a格式音乐处理)
//     * @author lid
//     * @date 2018.1.23
//     */
//    @ApiOperation("上传原创音乐文件(仅给IOS端提供m4a格式音乐处理)")
//    @RequestMapping(value = "/uploadOriginalMusic", method = RequestMethod.POST)
//    public ResultMessage<String> uploadOriginalMusic(@RequestPart("file") MultipartFile file,
//                                                     @RequestHeader(value = "Authorization") String token,
//                                                     String songLength) throws IOException {
//        String userId = JWTUtils.getTokenUserId(token);
//        return new ResultMessage<String>(fileService.uploadOriginalMusic(file, userId, songLength));
//    }

    /**
     * 第一步：上传原创音乐文件。(仅给IOS端提供m4a格式音乐处理)
     * @author lid
     * @date 2018.1.23
     */
    @ApiOperation("第一步：上传原创音乐文件，返回文件名")
    @RequestMapping(value = "/uploadOriginalMusic", method = RequestMethod.POST)
    public ResultMessage<FileInfo> uploadOriginalMusic(@RequestPart("file") MultipartFile file) throws IOException {
        return new ResultMessage<FileInfo>(fileService.uploadOriginalMusic(file));
    }

    /**
     * 第二步：保存原创音乐。(仅给IOS端提供m4a格式音乐处理)
     * @author lid
     * @date 2018.1.23
     */
    @ApiOperation("第二步：保存原创音乐")
    @RequestMapping(value = "/saveOriginalMusic", method = RequestMethod.POST)
    public ResultMessage<Musiclibrary> saveOriginalMusic(String pathName, String userId, String songLength){
        return new ResultMessage<Musiclibrary>(fileService.saveOriginalMusic(pathName, userId, songLength));
    }


    /**
     * 批量上传文件，返回文件信息，失败的文件名称为原文件名。
     */
    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    @ApiOperation("批量上传文件，上传成功的文件返回的文件名不为空")
    public ResultMessage<List<FileInfo>> uploadFile(@RequestPart("files") MultipartFile[] files) {
        List<FileInfo> fileInfos = new ArrayList<>(files.length);
        for (MultipartFile file : files) {
            try {
                FileInfo fileInfo = fileService.saveFile(file);
                fileInfos.add(fileInfo);
            } catch (Exception e) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setOriginalName(file.getOriginalFilename());
                fileInfos.add(fileInfo);
            }
        }
        return new ResultMessage<>(fileInfos);
    }

    /**
     * 删除文件，并返回被删除的文件信息。
     */
    @ApiOperation("删除文件，并返回被删除的文件信息")
    @RequestMapping(value = "/deleteFile", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<FileInfo> deleteFile(String fileId) {
        return new ResultMessage<>(fileService.deleteFile(fileId));
    }

    /**
     * 批量删除文件，返回被删除的文件信息，删除失败的文件只有ID信息。
     */
    @RequestMapping(value = "/deleteFiles", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("批量ID删除文件，删除成功的文件返回的文件名不为空")
    public ResultMessage<List<FileInfo>> deleteFiles(String[] fileIds) {
        List<FileInfo> fileInfos = new ArrayList<>(fileIds.length);
        for (String fileId : fileIds) {
            try {
                FileInfo fileInfo = fileService.deleteFile(fileId);
                fileInfos.add(fileInfo);
            } catch (Exception e) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setFileId(fileId);
                fileInfos.add(fileInfo);
            }
        }
        return new ResultMessage<>(fileInfos);
    }

    /**
     * 根据URL删除文件，并返回被删除的文件信息。
     */
    @ApiOperation("根据URL删除文件，URL必须是上传时返回的URL")
    @RequestMapping(value = "/deleteByUrl", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<FileInfo> deleteByUrl(String url) {
        return new ResultMessage<>(fileService.deleteByUrl(url));
    }

    /**
     * 根据URL批量删除文件，返回被删除的文件信息，删除失败的文件只有ID信息。
     */
    @RequestMapping(value = "/deleteByUrls", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("根据URL批量删除文件，删除成功的文件返回的文件名不为空")
    public ResultMessage<List<FileInfo>> deleteByUrls(String[] urls) {
        List<FileInfo> fileInfos = new ArrayList<>(urls.length);
        for (String url : urls) {
            try {
                FileInfo fileInfo = fileService.deleteByUrl(url);
                fileInfos.add(fileInfo);
            } catch (Exception e) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setFileId(url);
                fileInfos.add(fileInfo);
            }
        }
        return new ResultMessage<>(fileInfos);
    }
    @RequestMapping(value = "/videoCutFrame", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("视频截帧并上传")
    public ResultMessage videoCutFrame(String videoUrl,String videoId,String musicUrl) throws IOException {
        fileService.videoCutFrame(videoUrl,videoId,musicUrl);
		return new ResultMessage();
	}
    
    @RequestMapping(value = "/getvideoScreenImgs", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("获取视频截图")
    public ResultMessage<VideoScreenShotImg> getvideoScreenImgs(String videoId) throws IOException {
		return new ResultMessage<>(fileService.getVideoScreenImgs(videoId));
	}

}
